<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>Protocol Buffers | 课程简介</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../C08-RPC/RecoSysIDL.html" />
    
    
    <link rel="prev" href="../C08-RPC/gRPC.html" />
    

        
    </head>
    <body>
        
        
    <div class="book"
        data-level="8.3"
        data-chapter-title="Protocol Buffers"
        data-filepath="C08-RPC/ProtocolBuffers.md"
        data-basepath=".."
        data-revision="Mon May 27 2019 03:35:45 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        课程简介
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="C01-Introduction/index.html">
            
                
                    <a href="../C01-Introduction/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        项目介绍
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="C01-Introduction/AboutToutiaoWebVM.html">
            
                
                    <a href="../C01-Introduction/AboutToutiaoWebVM.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        ToutiaoWeb虚拟机使用说明
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="C01-Introduction/PycharmRemoteDevelop.html">
            
                
                    <a href="../C01-Introduction/PycharmRemoteDevelop.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        Pycharm远程开发
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="C01-Introduction/AboutProject.html">
            
                
                    <a href="../C01-Introduction/AboutProject.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        产品与开发
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="C02-Database/index.html">
            
                
                    <a href="../C02-Database/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        数据库
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="C02-Database/DatabaseDesign.html">
            
                
                    <a href="../C02-Database/DatabaseDesign.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        数据库设计
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="C02-Database/ORM.html">
            
                
                    <a href="../C02-Database/ORM.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        理解ORM
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="C02-Database/SQLAlchemyMapping.html">
            
                
                    <a href="../C02-Database/SQLAlchemyMapping.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        SQLAlchemy映射构建
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="C02-Database/SQLAlchemyOperations.html">
            
                
                    <a href="../C02-Database/SQLAlchemyOperations.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        SQLAlchemy操作
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="C02-Database/DatabaseTheory.html">
            
                
                    <a href="../C02-Database/DatabaseTheory.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                        数据库理论
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="C02-Database/DistributedID.html">
            
                
                    <a href="../C02-Database/DistributedID.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.6.</b>
                        
                        分布式ID
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="C02-Database/DatabaseOptimize.html">
            
                
                    <a href="../C02-Database/DatabaseOptimize.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.7.</b>
                        
                        数据库优化
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.8" data-path="C02-Database/Redis.html">
            
                
                    <a href="../C02-Database/Redis.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.8.</b>
                        
                        Redis
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="C03-Gitflow/index.html">
            
                
                    <a href="../C03-Gitflow/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        Git工作流
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="C03-Gitflow/Gitflow.html">
            
                
                    <a href="../C03-Gitflow/Gitflow.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        Gitflow
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="C03-Gitflow/AboutGit.html">
            
                
                    <a href="../C03-Gitflow/AboutGit.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        Git总结
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="C03-Gitflow/ProjectDirectory.html">
            
                
                    <a href="../C03-Gitflow/ProjectDirectory.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        工程目录
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="C03-Gitflow/DebugMethod.html">
            
                
                    <a href="../C03-Gitflow/DebugMethod.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        调试方法
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="C04-JWT/index.html">
            
                
                    <a href="../C04-JWT/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        JWT
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="C04-JWT/JWT.html">
            
                
                    <a href="../C04-JWT/JWT.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        JWT &amp; JWS &amp; JWE
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="C04-JWT/PythonLib.html">
            
                
                    <a href="../C04-JWT/PythonLib.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        PythonLib
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="C04-JWT/Implement.html">
            
                
                    <a href="../C04-JWT/Implement.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        实施方案
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="C04-JWT/Forbbiden.html">
            
                
                    <a href="../C04-JWT/Forbbiden.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.</b>
                        
                        禁用问题
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="C05-OSS/index.html">
            
                
                    <a href="../C05-OSS/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        对象存储
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="C05-OSS/Qiniu.html">
            
                
                    <a href="../C05-OSS/Qiniu.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        七牛云存储
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="C05-OSS/CDN.html">
            
                
                    <a href="../C05-OSS/CDN.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        CDN
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6" data-path="C06-Cache/index.html">
            
                
                    <a href="../C06-Cache/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        缓存
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.1" data-path="C06-Cache/CacheArchitecture.html">
            
                
                    <a href="../C06-Cache/CacheArchitecture.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.1.</b>
                        
                        缓存的架构
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.2" data-path="C06-Cache/CacheData.html">
            
                
                    <a href="../C06-Cache/CacheData.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.2.</b>
                        
                        缓存的数据
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.3" data-path="C06-Cache/CacheTTLAndEviction.html">
            
                
                    <a href="../C06-Cache/CacheTTLAndEviction.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.3.</b>
                        
                        缓存的有效期与淘汰策略
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.4" data-path="C06-Cache/CachePatterns.html">
            
                
                    <a href="../C06-Cache/CachePatterns.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.4.</b>
                        
                        缓存模式
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.5" data-path="C06-Cache/CacheProblems.html">
            
                
                    <a href="../C06-Cache/CacheProblems.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.5.</b>
                        
                        缓存问题
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.6" data-path="C06-Cache/ProjectCacheAndStorageDesign.html">
            
                
                    <a href="../C06-Cache/ProjectCacheAndStorageDesign.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.6.</b>
                        
                        项目缓存与Redis持久存储设计
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.7" data-path="C06-Cache/ProjectCacheImplement.html">
            
                
                    <a href="../C06-Cache/ProjectCacheImplement.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.7.</b>
                        
                        项目缓存实现
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.8" data-path="C06-Cache/ProjectRedisStorageImplement.html">
            
                
                    <a href="../C06-Cache/ProjectRedisStorageImplement.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.8.</b>
                        
                        项目Redis持久存储实现
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="7" data-path="C07-APScheduler/index.html">
            
                
                    <a href="../C07-APScheduler/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        APScheduler
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="7.1" data-path="C07-APScheduler/APSchedulerTutorial.html">
            
                
                    <a href="../C07-APScheduler/APSchedulerTutorial.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.1.</b>
                        
                        APScheduler使用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.2" data-path="C07-APScheduler/FixStatistics.html">
            
                
                    <a href="../C07-APScheduler/FixStatistics.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.2.</b>
                        
                        定时修正统计数据
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="8" data-path="C08-RPC/index.html">
            
                
                    <a href="../C08-RPC/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                        RPC
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="8.1" data-path="C08-RPC/AboutRPC.html">
            
                
                    <a href="../C08-RPC/AboutRPC.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.1.</b>
                        
                        RPC简介
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.2" data-path="C08-RPC/gRPC.html">
            
                
                    <a href="../C08-RPC/gRPC.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.2.</b>
                        
                        gRPC
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="8.3" data-path="C08-RPC/ProtocolBuffers.html">
            
                
                    <a href="../C08-RPC/ProtocolBuffers.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.3.</b>
                        
                        Protocol Buffers
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.4" data-path="C08-RPC/RecoSysIDL.html">
            
                
                    <a href="../C08-RPC/RecoSysIDL.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.4.</b>
                        
                        推荐系统接口定义
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.5" data-path="C08-RPC/Server.html">
            
                
                    <a href="../C08-RPC/Server.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.5.</b>
                        
                        服务端
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.6" data-path="C08-RPC/Client.html">
            
                
                    <a href="../C08-RPC/Client.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.6.</b>
                        
                        客户端
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.7" data-path="C08-RPC/ToutiaoAPIViewImplement.html">
            
                
                    <a href="../C08-RPC/ToutiaoAPIViewImplement.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.7.</b>
                        
                        头条首页新闻推荐接口编写
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="9" data-path="C09-IM/index.html">
            
                
                    <a href="../C09-IM/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.</b>
                        
                        即时通信
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="9.1" data-path="C09-IM/AboutIM.html">
            
                
                    <a href="../C09-IM/AboutIM.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.1.</b>
                        
                        即时通信简介
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9.2" data-path="C09-IM/WebSocket.html">
            
                
                    <a href="../C09-IM/WebSocket.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.2.</b>
                        
                        WebSocket
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9.3" data-path="C09-IM/SocketIO.html">
            
                
                    <a href="../C09-IM/SocketIO.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.3.</b>
                        
                        Socket.IO
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9.4" data-path="C09-IM/Chat.html">
            
                
                    <a href="../C09-IM/Chat.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.4.</b>
                        
                        头条聊天服务实现
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9.5" data-path="C09-IM/MessagePush.html">
            
                
                    <a href="../C09-IM/MessagePush.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.5.</b>
                        
                        头条在线消息推送实现
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="10" data-path="C10-Elasticsearch/index.html">
            
                
                    <a href="../C10-Elasticsearch/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.</b>
                        
                        Elasticsearch
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="10.1" data-path="C10-Elasticsearch/Introduction.html">
            
                
                    <a href="../C10-Elasticsearch/Introduction.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.1.</b>
                        
                        简介与原理
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.2" data-path="C10-Elasticsearch/ConceptAndCluster.html">
            
                
                    <a href="../C10-Elasticsearch/ConceptAndCluster.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.2.</b>
                        
                        概念与集群
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.3" data-path="C10-Elasticsearch/AnalysisIK.html">
            
                
                    <a href="../C10-Elasticsearch/AnalysisIK.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.3.</b>
                        
                        IK中文分析器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.4" data-path="C10-Elasticsearch/IndexAndType.html">
            
                
                    <a href="../C10-Elasticsearch/IndexAndType.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.4.</b>
                        
                        索引与类型
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.5" data-path="C10-Elasticsearch/Document.html">
            
                
                    <a href="../C10-Elasticsearch/Document.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.5.</b>
                        
                        文档
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.6" data-path="C10-Elasticsearch/Logstash.html">
            
                
                    <a href="../C10-Elasticsearch/Logstash.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.6.</b>
                        
                        Logstash导入数据
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.7" data-path="C10-Elasticsearch/Query.html">
            
                
                    <a href="../C10-Elasticsearch/Query.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.7.</b>
                        
                        查询
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.8" data-path="C10-Elasticsearch/ToutiaoSearchImplement.html">
            
                
                    <a href="../C10-Elasticsearch/ToutiaoSearchImplement.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.8.</b>
                        
                        头条全文检索实现
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.9" data-path="C10-Elasticsearch/Suggest.html">
            
                
                    <a href="../C10-Elasticsearch/Suggest.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.9.</b>
                        
                        suggest查询
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.10" data-path="C10-Elasticsearch/ToutiaoSuggestImplement.html">
            
                
                    <a href="../C10-Elasticsearch/ToutiaoSuggestImplement.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.10.</b>
                        
                        头条suggest查询实现
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="11" data-path="C11-Unittest.html">
            
                
                    <a href="../C11-Unittest.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>11.</b>
                        
                        单元测试
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="12" data-path="C12-Deployment/index.html">
            
                
                    <a href="../C12-Deployment/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>12.</b>
                        
                        部署相关
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="12.1" data-path="C12-Deployment/Gunicorn.html">
            
                
                    <a href="../C12-Deployment/Gunicorn.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>12.1.</b>
                        
                        Gunicorn
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="12.2" data-path="C12-Deployment/Supervisor.html">
            
                
                    <a href="../C12-Deployment/Supervisor.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>12.2.</b>
                        
                        Supervisor
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >课程简介</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="protocol-buffers">Protocol Buffers</h1>
<p>Protocol Buffers &#x662F;&#x4E00;&#x79CD;&#x4E0E;&#x8BED;&#x8A00;&#x65E0;&#x5173;&#xFF0C;&#x5E73;&#x53F0;&#x65E0;&#x5173;&#x7684;&#x53EF;&#x6269;&#x5C55;&#x673A;&#x5236;&#xFF0C;&#x7528;&#x4E8E;&#x5E8F;&#x5217;&#x5316;&#x7ED3;&#x6784;&#x5316;&#x6570;&#x636E;&#x3002;&#x4F7F;&#x7528;Protocol Buffers &#x53EF;&#x4EE5;&#x4E00;&#x6B21;&#x5B9A;&#x4E49;&#x7ED3;&#x6784;&#x5316;&#x7684;&#x6570;&#x636E;&#xFF0C;&#x7136;&#x540E;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x7279;&#x6B8A;&#x751F;&#x6210;&#x7684;&#x6E90;&#x4EE3;&#x7801;&#x8F7B;&#x677E;&#x5730;&#x5728;&#x5404;&#x79CD;&#x6570;&#x636E;&#x6D41;&#x4E2D;&#x4F7F;&#x7528;&#x5404;&#x79CD;&#x8BED;&#x8A00;&#x7F16;&#x5199;&#x548C;&#x8BFB;&#x53D6;&#x7ED3;&#x6784;&#x5316;&#x6570;&#x636E;&#x3002;</p>
<p>&#x73B0;&#x5728;&#x6709;&#x8BB8;&#x591A;&#x6846;&#x67B6;&#x7B49;&#x5728;&#x4F7F;&#x7528;Protocol Buffers&#x3002;gRPC&#x4E5F;&#x662F;&#x57FA;&#x4E8E;Protocol Buffers&#x3002; Protocol Buffers &#x76EE;&#x524D;&#x6709;2&#x548C;3&#x4E24;&#x4E2A;&#x7248;&#x672C;&#x53F7;&#x3002;</p>
<p><strong>&#x5728;gRPC&#x4E2D;&#x63A8;&#x8350;&#x4F7F;&#x7528;proto3&#x7248;&#x672C;&#x3002;</strong></p>
<h2 id="1--&#x6587;&#x6863;&#x7ED3;&#x6784;">1  &#x6587;&#x6863;&#x7ED3;&#x6784;</h2>
<h4 id="1&#xFF09;-protocol-buffers&#x7248;&#x672C;">1&#xFF09; Protocol Buffers&#x7248;&#x672C;</h4>
<p>Protocol Buffers&#x6587;&#x6863;&#x7684;&#x7B2C;&#x4E00;&#x884C;&#x975E;&#x6CE8;&#x91CA;&#x884C;&#xFF0C;&#x4E3A;&#x7248;&#x672C;&#x7533;&#x660E;&#xFF0C;&#x4E0D;&#x586B;&#x5199;&#x7684;&#x8BDD;&#x9ED8;&#x8BA4;&#x4E3A;&#x7248;&#x672C;2&#x3002;</p>
<pre><code>syntax = &quot;proto3&quot;;
&#x6216;&#x8005;
syntax = &quot;proto2&quot;;
</code></pre><h4 id="2&#xFF09;package&#x5305;">2&#xFF09;Package&#x5305;</h4>
<p>Protocol Buffers &#x53EF;&#x4EE5;&#x58F0;&#x660E;package&#xFF0C;&#x6765;&#x9632;&#x6B62;&#x547D;&#x540D;&#x51B2;&#x7A81;&#x3002; Packages&#x662F;&#x53EF;&#x9009;&#x7684;&#x3002;</p>
<pre><code>package foo.bar;
message Open { ... }
</code></pre><p>&#x4F7F;&#x7528;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x4E5F;&#x8981;&#x52A0;&#x4E0A;&#x547D;&#x540D;&#x7A7A;&#x95F4;&#xFF0C;</p>
<pre><code>message Foo {
  ...
  foo.bar.Open open = 1;
  ...
}
</code></pre><p><strong>&#x6CE8;&#x610F;&#xFF1A;&#x5BF9;&#x4E8E;Python&#x800C;&#x8A00;&#xFF0C;<code>package</code>&#x4F1A;&#x88AB;&#x5FFD;&#x7565;&#x5904;&#x7406;&#xFF0C;&#x56E0;&#x4E3A;Python&#x4E2D;&#x7684;&#x5305;&#x662F;&#x4EE5;&#x6587;&#x4EF6;&#x76EE;&#x5F55;&#x6765;&#x5B9A;&#x4E49;&#x7684;&#x3002;</strong></p>
<h4 id="3&#xFF09;&#x5BFC;&#x5165;">3&#xFF09;&#x5BFC;&#x5165;</h4>
<p> Protocol Buffers &#x4E2D;&#x53EF;&#x4EE5;&#x5BFC;&#x5165;&#x5176;&#x5B83;&#x6587;&#x4EF6;&#x6D88;&#x606F;&#x7B49;&#xFF0C;&#x4E0E;Python&#x7684;import&#x7C7B;&#x4F3C;&#x3002;</p>
<pre><code>import &#x201C;myproject/other_protos.proto&#x201D;;
</code></pre><h4 id="4&#xFF09;&#x5B9A;&#x4E49;&#x5404;&#x79CD;&#x6D88;&#x606F;&#x548C;&#x670D;&#x52A1;">4&#xFF09;&#x5B9A;&#x4E49;&#x5404;&#x79CD;&#x6D88;&#x606F;&#x548C;&#x670D;&#x52A1;</h4>
<p>&#x6D88;&#x606F;messge&#x662F;&#x7528;&#x6765;&#x5B9A;&#x4E49;&#x6570;&#x636E;&#x7684;&#xFF0C;&#x670D;&#x52A1;service&#x662F;&#x7528;&#x6765;gRPC&#x7684;&#x65B9;&#x6CD5;&#x7684;&#x3002;</p>
<h2 id="2-&#x6CE8;&#x91CA;">2 &#x6CE8;&#x91CA;</h2>
<p>Protocol Buffers &#x63D0;&#x4F9B;&#x4EE5;&#x4E0B;&#x4E24;&#x79CD;&#x6CE8;&#x91CA;&#x65B9;&#x5F0F;&#x3002;</p>
<pre><code class="lang-c"><span class="hljs-comment">// &#x5355;&#x884C;&#x6CE8;&#x91CA;</span>
<span class="hljs-comment">//</span>
<span class="hljs-comment">//</span>
<span class="hljs-comment">//</span>
<span class="hljs-comment">/* 
&#x591A;&#x884C;&#x6CE8;&#x91CA; 



*/</span>
</code></pre>
<h2 id="3-&#x6570;&#x636E;&#x7C7B;&#x578B;">3 &#x6570;&#x636E;&#x7C7B;&#x578B;</h2>
<h3 id="31-&#x57FA;&#x672C;&#x6570;&#x636E;&#x7C7B;&#x578B;">3.1 &#x57FA;&#x672C;&#x6570;&#x636E;&#x7C7B;&#x578B;</h3>
<table>
<thead>
<tr>
<th style="text-align:left">.proto</th>
<th style="text-align:left">&#x8BF4;&#x660E;</th>
<th style="text-align:left">Python</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">double</td>
<td style="text-align:left"></td>
<td style="text-align:left">float</td>
</tr>
<tr>
<td style="text-align:left">float</td>
<td style="text-align:left"></td>
<td style="text-align:left">float</td>
</tr>
<tr>
<td style="text-align:left">int32</td>
<td style="text-align:left">&#x4F7F;&#x7528;&#x53D8;&#x957F;&#x7F16;&#x7801;&#xFF0C;&#x5BF9;&#x8D1F;&#x6570;&#x7F16;&#x7801;&#x6548;&#x7387;&#x4F4E;&#xFF0C; &#x5982;&#x679C;&#x4F60;&#x7684;&#x53D8;&#x91CF;&#x53EF;&#x80FD;&#x662F;&#x8D1F;&#x6570;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;sint32</td>
<td style="text-align:left">int</td>
</tr>
<tr>
<td style="text-align:left">int64</td>
<td style="text-align:left">&#x4F7F;&#x7528;&#x53D8;&#x957F;&#x7F16;&#x7801;&#xFF0C;&#x5BF9;&#x8D1F;&#x6570;&#x7F16;&#x7801;&#x6548;&#x7387;&#x4F4E;&#xFF0C;&#x5982;&#x679C;&#x4F60;&#x7684;&#x53D8;&#x91CF;&#x53EF;&#x80FD;&#x662F;&#x8D1F;&#x6570;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;sint64</td>
<td style="text-align:left">int/long</td>
</tr>
<tr>
<td style="text-align:left">uint32</td>
<td style="text-align:left">&#x4F7F;&#x7528;&#x53D8;&#x957F;&#x7F16;&#x7801;</td>
<td style="text-align:left">int/long</td>
</tr>
<tr>
<td style="text-align:left">uint64</td>
<td style="text-align:left">&#x4F7F;&#x7528;&#x53D8;&#x957F;&#x7F16;&#x7801;</td>
<td style="text-align:left">int/long</td>
</tr>
<tr>
<td style="text-align:left">sint32</td>
<td style="text-align:left">&#x4F7F;&#x7528;&#x53D8;&#x957F;&#x7F16;&#x7801;&#xFF0C;&#x5E26;&#x7B26;&#x53F7;&#x7684;int&#x7C7B;&#x578B;&#xFF0C;&#x5BF9;&#x8D1F;&#x6570;&#x7F16;&#x7801;&#x6BD4;int32&#x9AD8;&#x6548;</td>
<td style="text-align:left">int</td>
</tr>
<tr>
<td style="text-align:left">sint64</td>
<td style="text-align:left">&#x4F7F;&#x7528;&#x53D8;&#x957F;&#x7F16;&#x7801;&#xFF0C;&#x5E26;&#x7B26;&#x53F7;&#x7684;int&#x7C7B;&#x578B;&#xFF0C;&#x5BF9;&#x8D1F;&#x6570;&#x7F16;&#x7801;&#x6BD4;int64&#x9AD8;&#x6548;</td>
<td style="text-align:left">int/long</td>
</tr>
<tr>
<td style="text-align:left">fixed32</td>
<td style="text-align:left">4&#x5B57;&#x8282;&#x7F16;&#x7801;&#xFF0C; &#x5982;&#x679C;&#x53D8;&#x91CF;&#x7ECF;&#x5E38;&#x5927;&#x4E8E;2^{28} &#x7684;&#x8BDD;&#xFF0C;&#x4F1A;&#x6BD4;uint32&#x9AD8;&#x6548;</td>
<td style="text-align:left">int</td>
</tr>
<tr>
<td style="text-align:left">fixed64</td>
<td style="text-align:left">8&#x5B57;&#x8282;&#x7F16;&#x7801;&#xFF0C; &#x5982;&#x679C;&#x53D8;&#x91CF;&#x7ECF;&#x5E38;&#x5927;&#x4E8E;2^{56} &#x7684;&#x8BDD;&#xFF0C;&#x4F1A;&#x6BD4;uint64&#x9AD8;&#x6548;</td>
<td style="text-align:left">int/long</td>
</tr>
<tr>
<td style="text-align:left">sfixed32</td>
<td style="text-align:left">4&#x5B57;&#x8282;&#x7F16;&#x7801;</td>
<td style="text-align:left">int</td>
</tr>
<tr>
<td style="text-align:left">sfixed64</td>
<td style="text-align:left">8&#x5B57;&#x8282;&#x7F16;&#x7801;</td>
<td style="text-align:left">int/long</td>
</tr>
<tr>
<td style="text-align:left">bool</td>
<td style="text-align:left"></td>
<td style="text-align:left">bool</td>
</tr>
<tr>
<td style="text-align:left">string</td>
<td style="text-align:left">&#x5FC5;&#x987B;&#x5305;&#x542B;utf-8&#x7F16;&#x7801;&#x6216;&#x8005;7-bit ASCII text</td>
<td style="text-align:left">str</td>
</tr>
<tr>
<td style="text-align:left">bytes</td>
<td style="text-align:left">&#x4EFB;&#x610F;&#x7684;&#x5B57;&#x8282;&#x5E8F;&#x5217;</td>
<td style="text-align:left">str</td>
</tr>
</tbody>
</table>
<h3 id="32-&#x679A;&#x4E3E;">3.2 &#x679A;&#x4E3E;</h3>
<p>&#x5728; Proto Buffers &#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x5B9A;&#x4E49;&#x679A;&#x4E3E;&#x548C;&#x679A;&#x4E3E;&#x7C7B;&#x578B;&#xFF0C;</p>
<pre><code class="lang-protobuf"><span class="hljs-class"><span class="hljs-keyword">enum</span> <span class="hljs-title">Corpus</span> </span>{
<span class="hljs-constant">    UNIVERSAL</span> = <span class="hljs-number">0</span>;
<span class="hljs-constant">    WEB</span> = <span class="hljs-number">1</span>;
<span class="hljs-constant">    IMAGES</span> = <span class="hljs-number">2</span>;
<span class="hljs-constant">    LOCAL</span> = <span class="hljs-number">3</span>;
<span class="hljs-constant">    NEWS</span> = <span class="hljs-number">4</span>;
<span class="hljs-constant">    PRODUCTS</span> = <span class="hljs-number">5</span>;
<span class="hljs-constant">    VIDEO</span> = <span class="hljs-number">6</span>;
}
<span class="hljs-constant">Corpus corpus</span> = <span class="hljs-number">4</span>;
</code></pre>
<p>&#x679A;&#x4E3E;&#x5B9A;&#x4E49;&#x5728;&#x4E00;&#x4E2A;&#x6D88;&#x606F;&#x5185;&#x90E8;&#x6216;&#x6D88;&#x606F;&#x5916;&#x90E8;&#x90FD;&#x662F;&#x53EF;&#x4EE5;&#x7684;&#xFF0C;&#x5982;&#x679C;&#x679A;&#x4E3E;&#x662F; &#x5B9A;&#x4E49;&#x5728; message &#x5185;&#x90E8;&#xFF0C;&#x800C;&#x5176;&#x4ED6; message &#x53C8;&#x60F3;&#x4F7F;&#x7528;&#xFF0C;&#x90A3;&#x4E48;&#x53EF;&#x4EE5;&#x901A;&#x8FC7; MessageType.EnumType &#x7684;&#x65B9;&#x5F0F;&#x5F15;&#x7528;&#x3002;</p>
<p><strong>&#x5B9A;&#x4E49;&#x679A;&#x4E3E;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x6211;&#x4EEC;&#x8981;&#x4FDD;&#x8BC1;&#x7B2C;&#x4E00;&#x4E2A;&#x679A;&#x4E3E;&#x503C;&#x5FC5;&#x987B;&#x662F;0&#xFF0C;&#x679A;&#x4E3E;&#x503C;&#x4E0D;&#x80FD;&#x91CD;&#x590D;&#xFF0C;&#x9664;&#x975E;&#x4F7F;&#x7528; option allow_alias = true &#x9009;&#x9879;&#x6765;&#x5F00;&#x542F;&#x522B;&#x540D;&#x3002;</strong></p>
<pre><code class="lang-protobuf"><span class="hljs-class"><span class="hljs-keyword">enum</span> <span class="hljs-title">EnumAllowingAlias</span> </span>{
    <span class="hljs-keyword">option</span> allow_alias = <span class="hljs-literal">true</span>;
<span class="hljs-constant">    UNKNOWN</span> = <span class="hljs-number">0</span>;
<span class="hljs-constant">    STARTED</span> = <span class="hljs-number">1</span>;
<span class="hljs-constant">    RUNNING</span> = <span class="hljs-number">1</span>;
}
</code></pre>
<p>&#x679A;&#x4E3E;&#x503C;&#x7684;&#x8303;&#x56F4;&#x662F;32-bit integer&#xFF0C;&#x4F46;&#x56E0;&#x4E3A;&#x679A;&#x4E3E;&#x503C;&#x4F7F;&#x7528;&#x53D8;&#x957F;&#x7F16;&#x7801;&#xFF0C;&#x6240;&#x4EE5;&#x4E0D;&#x63A8;&#x8350;&#x4F7F;&#x7528;&#x8D1F;&#x6570;&#x4F5C;&#x4E3A;&#x679A;&#x4E3E;&#x503C;&#xFF0C;&#x56E0;&#x4E3A;&#x8FD9;&#x4F1A;&#x5E26;&#x6765;&#x6548;&#x7387;&#x95EE;&#x9898;&#x3002;</p>
<h2 id="4-&#x6D88;&#x606F;&#x7C7B;&#x578B;">4 &#x6D88;&#x606F;&#x7C7B;&#x578B;</h2>
<p>Protocol Buffers&#x4F7F;&#x7528;message&#x5B9A;&#x4E49;&#x6D88;&#x606F;&#x6570;&#x636E;&#x3002;&#x5728;Protocol Buffers&#x4E2D;&#x4F7F;&#x7528;&#x7684;&#x6570;&#x636E;&#x90FD;&#x662F;&#x901A;&#x8FC7;message&#x6D88;&#x606F;&#x6570;&#x636E;&#x5C01;&#x88C5;&#x57FA;&#x672C;&#x7C7B;&#x578B;&#x6570;&#x636E;&#x6216;&#x5176;&#x4ED6;&#x6D88;&#x606F;&#x6570;&#x636E;&#xFF0C;&#x5BF9;&#x5E94;Python&#x4E2D;&#x7684;&#x7C7B;&#x3002;</p>
<pre><code class="lang-protobuf"><span class="hljs-class"><span class="hljs-keyword">message</span> <span class="hljs-title">SearchRequest</span> </span>{
  <span class="hljs-built_in">string</span> query = <span class="hljs-number">1</span>;
  <span class="hljs-built_in">int32</span> page_number = <span class="hljs-number">2</span>;
  <span class="hljs-built_in">int32</span> result_per_page = <span class="hljs-number">3</span>;
}
</code></pre>
<h3 id="41-&#x5B57;&#x6BB5;&#x7F16;&#x53F7;">4.1 &#x5B57;&#x6BB5;&#x7F16;&#x53F7;</h3>
<p>&#x6D88;&#x606F;&#x5B9A;&#x4E49;&#x4E2D;&#x7684;&#x6BCF;&#x4E2A;&#x5B57;&#x6BB5;&#x90FD;&#x6709;&#x552F;&#x4E00;&#x7684;&#x7F16;&#x53F7;&#x3002;<strong>&#x8FD9;&#x4E9B;&#x5B57;&#x6BB5;&#x7F16;&#x53F7;&#x7528;&#x4E8E;&#x4EE5;&#x6D88;&#x606F;&#x4E8C;&#x8FDB;&#x5236;&#x683C;&#x5F0F;&#x6807;&#x8BC6;&#x5B57;&#x6BB5;&#xFF0C;&#x5E76;&#x4E14;&#x5728;&#x4F7F;&#x7528;&#x6D88;&#x606F;&#x7C7B;&#x578B;&#x540E;&#x4E0D;&#x5E94;&#x66F4;&#x6539;&#x3002;</strong> &#x8BF7;&#x6CE8;&#x610F;&#xFF0C;<strong>1&#x5230;15&#x8303;&#x56F4;&#x5185;&#x7684;&#x5B57;&#x6BB5;&#x7F16;&#x53F7;&#x9700;&#x8981;&#x4E00;&#x4E2A;&#x5B57;&#x8282;&#x8FDB;&#x884C;&#x7F16;&#x7801;&#xFF0C;&#x5305;&#x62EC;&#x5B57;&#x6BB5;&#x7F16;&#x53F7;&#x548C;&#x5B57;&#x6BB5;&#x7C7B;&#x578B;</strong>&#x3002;<strong>16&#x5230;2047&#x8303;&#x56F4;&#x5185;&#x7684;&#x5B57;&#x6BB5;&#x7F16;&#x53F7;&#x5360;&#x7528;&#x4E24;&#x4E2A;&#x5B57;&#x8282;</strong>&#x3002;&#x56E0;&#x6B64;&#xFF0C;&#x60A8;&#x5E94;&#x8BE5;&#x4E3A;&#x975E;&#x5E38;&#x9891;&#x7E41;&#x51FA;&#x73B0;&#x7684;&#x6D88;&#x606F;&#x5143;&#x7D20;&#x4FDD;&#x7559;&#x6570;&#x5B57;1&#x5230;15&#x3002;&#x8BF7;&#x8BB0;&#x4F4F;&#x4E3A;&#x5C06;&#x6765;&#x53EF;&#x80FD;&#x6DFB;&#x52A0;&#x7684;&#x5E38;&#x7528;&#x5143;&#x7D20;&#x7559;&#x51FA;&#x4E00;&#x4E9B;&#x7A7A;&#x95F4;&#x3002;</p>
<p>&#x6700;&#x5C0F;&#x7684;&#x6807;&#x8BC6;&#x53F7;&#x53EF;&#x4EE5;&#x4ECE;1&#x5F00;&#x59CB;&#xFF0C;&#x6700;&#x5927;&#x5230;2^29 - 1,&#x6216; 536,870,911&#x3002;&#x4E0D;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x5176;&#x4E2D;&#x7684;[19000&#xFF0D;19999]&#x7684;&#x6807;&#x8BC6;&#x53F7;&#xFF0C; Protobuf&#x534F;&#x8BAE;&#x5B9E;&#x73B0;&#x4E2D;&#x5BF9;&#x8FD9;&#x4E9B;&#x8FDB;&#x884C;&#x4E86;&#x9884;&#x7559;&#x3002;&#x5982;&#x679C;&#x975E;&#x8981;&#x5728;.proto&#x6587;&#x4EF6;&#x4E2D;&#x4F7F;&#x7528;&#x8FD9;&#x4E9B;&#x9884;&#x7559;&#x6807;&#x8BC6;&#x53F7;&#xFF0C;&#x7F16;&#x8BD1;&#x65F6;&#x5C31;&#x4F1A;&#x62A5;&#x8B66;&#x3002;&#x540C;&#x6837;&#x4F60;&#x4E5F;&#x4E0D;&#x80FD;&#x4F7F;&#x7528;&#x65E9;&#x671F;&#x4FDD;&#x7559;&#x7684;&#x6807;&#x8BC6;&#x53F7;&#x3002;</p>
<h3 id="42-&#x6307;&#x5B9A;&#x5B57;&#x6BB5;&#x89C4;&#x5219;">4.2 &#x6307;&#x5B9A;&#x5B57;&#x6BB5;&#x89C4;&#x5219;</h3>
<p>&#x6D88;&#x606F;&#x5B57;&#x6BB5;&#x53EF;&#x4EE5;&#x662F;&#x4EE5;&#x4E0B;&#x4E4B;&#x4E00;&#xFF1A;</p>
<ul>
<li><p>singular&#xFF1A;&#x683C;&#x5F0F;&#x826F;&#x597D;&#x7684;&#x6D88;&#x606F;&#x53EF;&#x4EE5;&#x5305;&#x542B;&#x8BE5;&#x5B57;&#x6BB5;&#x4E2D;&#x7684;&#x96F6;&#x4E2A;&#x6216;&#x4E00;&#x4E2A;&#xFF08;&#x4F46;&#x4E0D;&#x8D85;&#x8FC7;&#x4E00;&#x4E2A;&#xFF09;&#x3002;</p>
</li>
<li><p>repeated&#xFF1A;&#x6B64;&#x5B57;&#x6BB5;&#x53EF;&#x4EE5;&#x5728;&#x683C;&#x5F0F;&#x826F;&#x597D;&#x7684;&#x6D88;&#x606F;&#x4E2D;&#x91CD;&#x590D;&#x4EFB;&#x610F;&#x6B21;&#x6570;&#xFF08;&#x5305;&#x62EC;&#x96F6;&#xFF09;&#x3002;&#x5C06;&#x4FDD;&#x7559;&#x91CD;&#x590D;&#x503C;&#x7684;&#x987A;&#x5E8F;&#x3002;&#x5BF9;&#x5E94;Python&#x7684;&#x5217;&#x8868;&#x3002;</p>
<pre><code class="lang-protobuf">  <span class="hljs-class"><span class="hljs-keyword">message</span> <span class="hljs-title">Result</span> </span>{
    <span class="hljs-built_in">string</span> url = <span class="hljs-number">1</span>;
    <span class="hljs-built_in">string</span> title = <span class="hljs-number">2</span>;
    <span class="hljs-keyword">repeated</span> <span class="hljs-built_in">string</span> snippets = <span class="hljs-number">3</span>;
  }
</code></pre>
</li>
</ul>
<h3 id="43-&#x6DFB;&#x52A0;&#x66F4;&#x591A;&#x6D88;&#x606F;&#x7C7B;&#x578B;">4.3 &#x6DFB;&#x52A0;&#x66F4;&#x591A;&#x6D88;&#x606F;&#x7C7B;&#x578B;</h3>
<p>&#x53EF;&#x4EE5;&#x5728;&#x5355;&#x4E2A;.proto&#x6587;&#x4EF6;&#x4E2D;&#x5B9A;&#x4E49;&#x591A;&#x4E2A;&#x6D88;&#x606F;&#x7C7B;&#x578B;&#x3002;</p>
<pre><code class="lang-protobuf"><span class="hljs-class"><span class="hljs-keyword">message</span> <span class="hljs-title">SearchRequest</span> </span>{
  <span class="hljs-built_in">string</span> query = <span class="hljs-number">1</span>;
  <span class="hljs-built_in">int32</span> page_number = <span class="hljs-number">2</span>;
  <span class="hljs-built_in">int32</span> result_per_page = <span class="hljs-number">3</span>;
}

<span class="hljs-class"><span class="hljs-keyword">message</span> <span class="hljs-title">SearchResponse</span> </span>{
 ...
}
</code></pre>
<h3 id="44-&#x4FDD;&#x7559;&#x5B57;&#x6BB5;">4.4 &#x4FDD;&#x7559;&#x5B57;&#x6BB5;</h3>
<p>&#x4FDD;&#x7559;&#x53D8;&#x91CF;&#x4E0D;&#x88AB;&#x4F7F;&#x7528;</p>
<p>&#x5982;&#x679C;&#x901A;&#x8FC7;&#x5B8C;&#x5168;&#x5220;&#x9664;&#x5B57;&#x6BB5;&#x6216;&#x5C06;&#x5176;&#x6CE8;&#x91CA;&#x6765;&#x66F4;&#x65B0;&#x6D88;&#x606F;&#x7C7B;&#x578B;&#xFF0C;&#x5219;&#x672A;&#x6765;&#x7528;&#x6237;&#x53EF;&#x4EE5;&#x5728;&#x5BF9;&#x7C7B;&#x578B;&#x8FDB;&#x884C;&#x81EA;&#x5DF1;&#x7684;&#x66F4;&#x65B0;&#x65F6;&#x91CD;&#x7528;&#x5B57;&#x6BB5;&#x7F16;&#x53F7;&#x3002;&#x5982;&#x679C;&#x4EE5;&#x540E;&#x52A0;&#x8F7D;&#x76F8;&#x540C;&#x7684;&#x65E7;&#x7248;&#x672C;&#xFF0C;&#x8FD9;&#x53EF;&#x80FD;&#x4F1A;&#x5BFC;&#x81F4;&#x4E25;&#x91CD;&#x95EE;&#x9898;&#xFF0C;&#x5305;&#x62EC;&#x6570;&#x636E;&#x635F;&#x574F;&#xFF0C;&#x9690;&#x79C1;&#x9519;&#x8BEF;&#x7B49;&#x3002;&#x786E;&#x4FDD;&#x4E0D;&#x4F1A;&#x53D1;&#x751F;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#x7684;&#x4E00;&#x79CD;&#x65B9;&#x6CD5;&#x662F;<strong>&#x6307;&#x5B9A;&#x5DF2;&#x5220;&#x9664;&#x5B57;&#x6BB5;&#x7684;&#x5B57;&#x6BB5;&#x7F16;&#x53F7;&#xFF08;&#x6216;&#x540D;&#x79F0;&#xFF09;reserved</strong>&#x3002;&#x5982;&#x679C;&#x5C06;&#x6765;&#x7684;&#x4EFB;&#x4F55;&#x7528;&#x6237;&#x5C1D;&#x8BD5;&#x4F7F;&#x7528;&#x8FD9;&#x4E9B;&#x5B57;&#x6BB5;&#x6807;&#x8BC6;&#x7B26;&#xFF0C;protobuf&#x7F16;&#x8BD1;&#x5668;&#x5C06;&#x4F1A;&#x62A5;&#x9519;&#x3002;</p>
<pre><code class="lang-protobuf"><span class="hljs-class"><span class="hljs-keyword">message</span> <span class="hljs-title">Foo</span> </span>{
  reserved <span class="hljs-number">2</span>, <span class="hljs-number">15</span>, <span class="hljs-number">9</span> to <span class="hljs-number">11</span>;
  reserved <span class="hljs-string">&quot;foo&quot;</span>, <span class="hljs-string">&quot;bar&quot;</span>;
}
</code></pre>
<h3 id="45-&#x9ED8;&#x8BA4;&#x503C;">4.5 &#x9ED8;&#x8BA4;&#x503C;</h3>
<p>&#x89E3;&#x6790;&#x6D88;&#x606F;&#x65F6;&#xFF0C;&#x5982;&#x679C;&#x7F16;&#x7801;&#x6D88;&#x606F;&#x4E0D;&#x5305;&#x542B;&#x7279;&#x5B9A;&#x7684;&#x5355;&#x6570;&#x5143;&#x7D20;&#xFF0C;&#x5219;&#x89E3;&#x6790;&#x5BF9;&#x8C61;&#x4E2D;&#x7684;&#x76F8;&#x5E94;&#x5B57;&#x6BB5;&#x5C06;&#x8BBE;&#x7F6E;&#x4E3A;&#x8BE5;&#x5B57;&#x6BB5;&#x7684;&#x9ED8;&#x8BA4;&#x503C;&#x3002;&#x8FD9;&#x4E9B;&#x9ED8;&#x8BA4;&#x503C;&#x662F;&#x7279;&#x5B9A;&#x4E8E;&#x7C7B;&#x578B;&#x7684;&#xFF1A;</p>
<ul>
<li>&#x5BF9;&#x4E8E;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x9ED8;&#x8BA4;&#x503C;&#x4E3A;&#x7A7A;&#x5B57;&#x7B26;&#x4E32;&#x3002;</li>
<li>&#x5BF9;&#x4E8E;&#x5B57;&#x8282;&#xFF0C;&#x9ED8;&#x8BA4;&#x503C;&#x4E3A;&#x7A7A;&#x5B57;&#x8282;&#x3002;</li>
<li>&#x5BF9;&#x4E8E;bools&#xFF0C;&#x9ED8;&#x8BA4;&#x503C;&#x4E3A;false&#x3002;</li>
<li>&#x5BF9;&#x4E8E;&#x6570;&#x5B57;&#x7C7B;&#x578B;&#xFF0C;&#x9ED8;&#x8BA4;&#x503C;&#x4E3A;&#x96F6;&#x3002;</li>
<li>&#x5BF9;&#x4E8E;&#x679A;&#x4E3E;&#xFF0C;&#x9ED8;&#x8BA4;&#x503C;&#x662F;&#x7B2C;&#x4E00;&#x4E2A;&#x5B9A;&#x4E49;&#x7684;&#x679A;&#x4E3E;&#x503C;&#xFF0C;&#x8BE5;&#x503C;&#x5FC5;&#x987B;&#x4E3A;0&#x3002;</li>
<li>&#x5BF9;&#x4E8E;&#x6D88;&#x606F;&#x5B57;&#x6BB5;&#xFF0C;&#x672A;&#x8BBE;&#x7F6E;&#x8BE5;&#x5B57;&#x6BB5;&#x3002;&#x5B83;&#x7684;&#x786E;&#x5207;&#x503C;&#x53D6;&#x51B3;&#x4E8E;&#x8BED;&#x8A00;&#x3002;</li>
<li>&#x91CD;&#x590D;&#x5B57;&#x6BB5;&#x7684;&#x9ED8;&#x8BA4;&#x503C;&#x4E3A;&#x7A7A;&#xFF08;&#x901A;&#x5E38;&#x662F;&#x76F8;&#x5E94;&#x8BED;&#x8A00;&#x7684;&#x7A7A;&#x5217;&#x8868;&#xFF09;&#x3002;</li>
</ul>
<h3 id="46-&#x5D4C;&#x5957;&#x7C7B;&#x578B;">4.6 &#x5D4C;&#x5957;&#x7C7B;&#x578B;</h3>
<p>&#x4F60;&#x53EF;&#x4EE5;&#x5728;&#x5176;&#x4ED6;&#x6D88;&#x606F;&#x7C7B;&#x578B;&#x4E2D;&#x5B9A;&#x4E49;&#x3001;&#x4F7F;&#x7528;&#x6D88;&#x606F;&#x7C7B;&#x578B;&#xFF0C;&#x5728;&#x4E0B;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x4E2D;&#xFF0C;Result&#x6D88;&#x606F;&#x5C31;&#x5B9A;&#x4E49;&#x5728;SearchResponse&#x6D88;&#x606F;&#x5185;&#xFF0C;&#x5982;&#xFF1A;</p>
<pre><code class="lang-protobuf"><span class="hljs-class"><span class="hljs-keyword">message</span> <span class="hljs-title">SearchResponse</span> </span>{
  <span class="hljs-class"><span class="hljs-keyword">message</span> <span class="hljs-title">Result</span> </span>{
    <span class="hljs-built_in">string</span> url = <span class="hljs-number">1</span>;
    <span class="hljs-built_in">string</span> title = <span class="hljs-number">2</span>;
    <span class="hljs-keyword">repeated</span> <span class="hljs-built_in">string</span> snippets = <span class="hljs-number">3</span>;
  }
  <span class="hljs-keyword">repeated</span> Result results = <span class="hljs-number">1</span>;
}
</code></pre>
<p>&#x5982;&#x679C;&#x8981;&#x5728;&#x5176;&#x7236;&#x6D88;&#x606F;&#x7C7B;&#x578B;&#x4E4B;&#x5916;&#x91CD;&#x7528;&#x6B64;&#x6D88;&#x606F;&#x7C7B;&#x578B;&#xFF0C;&#x4F7F;&#x7528;</p>
<pre><code class="lang-protobuf"><span class="hljs-constant">SearchResponse.Result
</span></code></pre>
<h2 id="5-map&#x6620;&#x5C04;">5 map&#x6620;&#x5C04;</h2>
<p>&#x5982;&#x679C;&#x8981;&#x5728;&#x6570;&#x636E;&#x5B9A;&#x4E49;&#x4E2D;&#x521B;&#x5EFA;&#x5173;&#x8054;&#x6620;&#x5C04;&#xFF0C;Protocol Buffers&#x63D0;&#x4F9B;&#x4E86;&#x4E00;&#x79CD;&#x65B9;&#x4FBF;&#x7684;&#x8BED;&#x6CD5;&#xFF1A;</p>
<pre><code>map&lt; key_type, value_type&gt; map_field = N ;
</code></pre><p>&#x5176;&#x4E2D;key_type&#x53EF;&#x4EE5;&#x662F;&#x4EFB;&#x4F55;&#x6574;&#x6570;&#x6216;&#x5B57;&#x7B26;&#x4E32;&#x7C7B;&#x578B;&#x3002;&#x8BF7;&#x6CE8;&#x610F;&#xFF0C;&#x679A;&#x4E3E;&#x4E0D;&#x662F;&#x6709;&#x6548;&#x7684;key_type&#x3002;value_type&#x53EF;&#x4EE5;&#x662F;&#x9664;map&#x6620;&#x5C04;&#x7C7B;&#x578B;&#x5916;&#x7684;&#x4EFB;&#x4F55;&#x7C7B;&#x578B;&#x3002;</p>
<p>&#x4F8B;&#x5982;&#xFF0C;&#x5982;&#x679C;&#x8981;&#x521B;&#x5EFA;&#x9879;&#x76EE;&#x6620;&#x5C04;&#xFF0C;&#x5176;&#x4E2D;&#x6BCF;&#x6761;Project&#x6D88;&#x606F;&#x90FD;&#x4E0E;&#x5B57;&#x7B26;&#x4E32;&#x952E;&#x76F8;&#x5173;&#x8054;&#xFF0C;&#x5219;&#x53EF;&#x4EE5;&#x50CF;&#x4E0B;&#x9762;&#x8FD9;&#x6837;&#x5B9A;&#x4E49;&#x5B83;&#xFF1A;</p>
<pre><code>map&lt;string, Project&gt; projects = 3 ;
</code></pre><ul>
<li>map&#x7684;&#x5B57;&#x6BB5;&#x53EF;&#x4EE5;&#x662F;repeated&#x3002;</li>
<li>&#x5E8F;&#x5217;&#x5316;&#x540E;&#x7684;&#x987A;&#x5E8F;&#x548C;map&#x8FED;&#x4EE3;&#x5668;&#x7684;&#x987A;&#x5E8F;&#x662F;&#x4E0D;&#x786E;&#x5B9A;&#x7684;&#xFF0C;&#x6240;&#x4EE5;&#x4F60;&#x4E0D;&#x8981;&#x671F;&#x671B;&#x4EE5;&#x56FA;&#x5B9A;&#x987A;&#x5E8F;&#x5904;&#x7406;map</li>
<li>&#x5F53;&#x4E3A;.proto&#x6587;&#x4EF6;&#x4EA7;&#x751F;&#x751F;&#x6210;&#x6587;&#x672C;&#x683C;&#x5F0F;&#x7684;&#x65F6;&#x5019;&#xFF0C;map&#x4F1A;&#x6309;&#x7167;key &#x7684;&#x987A;&#x5E8F;&#x6392;&#x5E8F;&#xFF0C;&#x6570;&#x503C;&#x5316;&#x7684;key&#x4F1A;&#x6309;&#x7167;&#x6570;&#x503C;&#x6392;&#x5E8F;&#x3002;</li>
<li>&#x4ECE;&#x5E8F;&#x5217;&#x5316;&#x4E2D;&#x89E3;&#x6790;&#x6216;&#x8005;&#x878D;&#x5408;&#x65F6;&#xFF0C;&#x5982;&#x679C;&#x6709;&#x91CD;&#x590D;&#x7684;key&#x5219;&#x540E;&#x4E00;&#x4E2A;key&#x4E0D;&#x4F1A;&#x88AB;&#x4F7F;&#x7528;&#xFF0C;&#x5F53;&#x4ECE;&#x6587;&#x672C;&#x683C;&#x5F0F;&#x4E2D;&#x89E3;&#x6790;map&#x65F6;&#xFF0C;&#x5982;&#x679C;&#x5B58;&#x5728;&#x91CD;&#x590D;&#x7684;key&#xFF0C;&#x5219;&#x89E3;&#x6790;&#x53EF;&#x80FD;&#x4F1A;&#x5931;&#x8D25;&#x3002;</li>
<li>&#x5982;&#x679C;&#x4E3A;&#x6620;&#x5C04;&#x5B57;&#x6BB5;&#x63D0;&#x4F9B;&#x952E;&#x4F46;&#x6CA1;&#x6709;&#x503C;&#xFF0C;&#x5219;&#x5B57;&#x6BB5;&#x5E8F;&#x5217;&#x5316;&#x65F6;&#x7684;&#x884C;&#x4E3A;&#x53D6;&#x51B3;&#x4E8E;&#x8BED;&#x8A00;&#x3002;&#x5728;Python&#x4E2D;&#xFF0C;&#x4F7F;&#x7528;&#x7C7B;&#x578B;&#x7684;&#x9ED8;&#x8BA4;&#x503C;&#x3002;</li>
</ul>
<h2 id="6-oneof">6 oneof</h2>
<p>&#x5982;&#x679C;&#x4F60;&#x7684;&#x6D88;&#x606F;&#x4E2D;&#x6709;&#x5F88;&#x591A;&#x53EF;&#x9009;&#x5B57;&#x6BB5;&#xFF0C; &#x5E76;&#x4E14;&#x540C;&#x65F6;&#x81F3;&#x591A;&#x4E00;&#x4E2A;&#x5B57;&#x6BB5;&#x4F1A;&#x88AB;&#x8BBE;&#x7F6E;&#xFF0C; &#x4F60;&#x53EF;&#x4EE5;&#x52A0;&#x5F3A;&#x8FD9;&#x4E2A;&#x884C;&#x4E3A;&#xFF0C;&#x4F7F;&#x7528;oneof&#x7279;&#x6027;&#x8282;&#x7701;&#x5185;&#x5B58;&#x3002;</p>
<p>&#x4E3A;&#x4E86;&#x5728;.proto&#x5B9A;&#x4E49;oneof&#x5B57;&#x6BB5;&#xFF0C; &#x4F60;&#x9700;&#x8981;&#x5728;&#x540D;&#x5B57;&#x524D;&#x9762;&#x52A0;&#x4E0A;oneof&#x5173;&#x952E;&#x5B57;, &#x6BD4;&#x5982;&#x4E0B;&#x9762;&#x4F8B;&#x5B50;&#x7684;test_oneof:</p>
<pre><code class="lang-protobuf"><span class="hljs-class"><span class="hljs-keyword">message</span> <span class="hljs-title">SampleMessage</span> </span>{
  oneof test_oneof {
    <span class="hljs-built_in">string</span> name = <span class="hljs-number">4</span>;
<span class="hljs-constant">    SubMessage sub_message</span> = <span class="hljs-number">9</span>;
  }
}
</code></pre>
<p>&#x7136;&#x540E;&#x4F60;&#x53EF;&#x4EE5;&#x589E;&#x52A0;oneof&#x5B57;&#x6BB5;&#x5230; oneof &#x5B9A;&#x4E49;&#x4E2D;. &#x4F60;&#x53EF;&#x4EE5;&#x589E;&#x52A0;&#x4EFB;&#x610F;&#x7C7B;&#x578B;&#x7684;&#x5B57;&#x6BB5;, &#x4F46;&#x662F;&#x4E0D;&#x80FD;&#x4F7F;&#x7528;repeated &#x5173;&#x952E;&#x5B57;&#x3002;</p>
<h2 id="7-&#x5B9A;&#x4E49;&#x670D;&#x52A1;">7 &#x5B9A;&#x4E49;&#x670D;&#x52A1;</h2>
<p>Protocol Buffers&#x4F7F;&#x7528;service&#x5B9A;&#x4E49;RPC&#x670D;&#x52A1;&#x3002;</p>
<pre><code class="lang-protobuf"><span class="hljs-class"><span class="hljs-keyword">message</span> <span class="hljs-title">HelloRequest</span> </span>{
  <span class="hljs-built_in">string</span> greeting = <span class="hljs-number">1</span>;
}

<span class="hljs-class"><span class="hljs-keyword">message</span> <span class="hljs-title">HelloResponse</span> </span>{
  <span class="hljs-built_in">string</span> reply = <span class="hljs-number">1</span>;
}

<span class="hljs-class"><span class="hljs-keyword">service</span> <span class="hljs-title">HelloService</span> </span>{
  <span class="hljs-function"><span class="hljs-keyword">rpc</span> SayHello (HelloRequest) <span class="hljs-keyword">returns</span> (HelloResponse) {}
}
</span></code></pre>
<p><strong>&#x6CE8;&#x610F;&#xFF1A;&#x4E00;&#x4E2A;service&#x4E2D;&#x53EF;&#x5B9A;&#x4E49;&#x591A;&#x4E2A;&#x65B9;&#x6CD5;&#x3002;</strong></p>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../C08-RPC/gRPC.html" class="navigation navigation-prev " aria-label="Previous page: gRPC"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../C08-RPC/RecoSysIDL.html" class="navigation navigation-next " aria-label="Next page: 推荐系统接口定义"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
